﻿/* Scripts for match history partial view */

$(function () {
    var masterHub = $.connection.masterHub;

    masterHub.client.updateMatchHistory = function (data) {

        // Clear any previous game displays.
        $(".singleMatch").remove();

        // Plot up new game displays.
        for (var i = 0; i < data.length; i++) {
            var playerNames = "-";
            for (var j = 0; j < data[i].playerUserProfiles.length; j++) {
                playerNames += "<a href='/Account/UserProfile/" + data[i].playerUserProfiles[j].UserId + "'>" + data[i].playerUserProfiles[j].UserName + "</a>-";
            }

            $("#recentMatches").append("<div class='singleMatch'><img src = /Images/" + data[i].gameTypeGameID + ".png></img> <ul>"
                                + "<li><b>Players</b> :</li> <li>" + playerNames + "</li>"
                                + "<li><b>Game Type</b> : " + data[i].gameType.name + "</li>"
                                + "<li><i>" + data[i].gameEnded + "</i></li>"
                                + "</ul></div>");
        }
    };

    $.connection.hub.start().done(function () {
        masterHub.server.getMatchHistory($("#numberOfMatches").text(), $("#userId").text());

        // Set input number field.
        $("#numberOfInputField").val($("#numberOfMatches").text());

        // Connect enter keyup event to get match history.
        $("#numberOfInputField").keyup(function (e) {
            if (e.keyCode == 13) {

                masterHub.server.getMatchHistory($("#numberOfInputField").val(), $("#userId").text());

            }
        });

        // Connect arrow clicked event to get mach history.
        $("#numberOfInputField").click(function (e) {
            masterHub.server.getMatchHistory($("#numberOfInputField").val(), $("#userId").text());
        });
    });
}
);

